package longtd.app.game;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
	
	// Surface Holder ??? don't understand
	// Can call callback function (good)
	private SurfaceHolder holder;
	private long lastClick;
	
	private GameLoopThread gameLoopThread;

	
	// Init Sprite
	private List<Sprite> sprites = new ArrayList<Sprite>();
	private List<TempSprite>temps = new ArrayList<TempSprite>();
	private Bitmap bmpBlood;
	
	public GameView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		gameLoopThread = new GameLoopThread(this);	
		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				boolean retry = true;
				gameLoopThread.setRunning(false);
				while (retry){
					try{
						gameLoopThread.join();
						retry = false;
					} catch (InterruptedException e){
						
					}
				}
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				// when you drawing canvas, no other can draw
				createSprites();
				gameLoopThread.setRunning(true);
				gameLoopThread.start();
				
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				// TODO Auto-generated method stub
				
			}
		});
		bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);
		//this.setBackgroundResource(R.drawable.background1);
		
		
	}

	/**
	 * Add each sprite to ArrayList sprites
	 */
	private void createSprites(){
		sprites.add(createSprite(R.drawable.bad1));
        sprites.add(createSprite(R.drawable.bad2));
        sprites.add(createSprite(R.drawable.bad3));
        sprites.add(createSprite(R.drawable.bad4));
        sprites.add(createSprite(R.drawable.bad5));
        sprites.add(createSprite(R.drawable.bad6));
        sprites.add(createSprite(R.drawable.good1));
        sprites.add(createSprite(R.drawable.good2));
        sprites.add(createSprite(R.drawable.good3));
        sprites.add(createSprite(R.drawable.good4));
        sprites.add(createSprite(R.drawable.good5));
        sprites.add(createSprite(R.drawable.good6));
	}
	
	/**
	 * create one Sprite
	 * @param resource
	 * @return
	 */
	private Sprite createSprite(int resource){
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
		return new Sprite(this,bmp);
	}
	
	/**
	 *  Canvas like a blackboard, you can draw everything on it.
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		
		canvas.drawColor(Color.WHITE);
		//Bitmap bmpBackground = BitmapFactory.decodeResource(getResources(), R.drawable.background1);
		//canvas.drawBitmap(bmpBackground, 0, 0, null);
		
		for (int i = 0; i< temps.size() ;i++){
			temps.get(i).onDraw(canvas);
		}
		
		for(Sprite sprite: sprites){
			sprite.onDraw(canvas);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent e){
		if (System.currentTimeMillis() - lastClick > 300){
			lastClick = System.currentTimeMillis();
			float x  = e.getX();
			float y = e.getY();
			synchronized (getHolder()) {
				for (int i = sprites.size() - 1; i>=0; i--){
					Sprite sprite = sprites.get(i);
					if (sprite.isCollition(x, y)){
						sprites.remove(sprite);
						temps.add(new TempSprite(temps, this, x, y, bmpBlood));
						break;
						
					}
				}
			}
		}
		
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}

	
	
}
